package spec.zyh;

import java.sql.Connection;
import java.sql.DriverManager;

public class DatabaseConnetion {
    //定义数据库驱动程序
    private static final String DBDRIVER = "com.mysql.jdbc.Driver";
    //数据连接地址
    private static final String DBURL = "jdbc:mysql://localhost:3306/1613580003?characterEncoding=utf8";
    private static final String DBUSER = "root";
    private static final String DBPASS = "123456";
    private Connection connection = null;

    public DatabaseConnetion() throws Exception {
        try {
            //数据库操作困难出现异常
            Class.forName(DBDRIVER);
            connection = DriverManager.getConnection(DBURL, DBDRIVER, DBPASS);
        } catch (Exception exception) {
            throw exception;
        } finally {
        }

    }
    public Connection getConnection() {
        return connection;
    }

    public void close() throws Exception {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
                throw e;
            }
        }
    }
}
